﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DARE.Save.DataStructures
{
    public class CEntityData : ISaveData
    {
        public AModelData modelData;
        public CPhysicObjectData physicObjectData;
        public List<CEmitterData> emitterData = new List<CEmitterData>();
#if !WINDOWS_PHONE
        public List<CAudioEntityData> audioEntityData = new List<CAudioEntityData>();
#endif
        public List<CSprite3DData> sprite3DData = new List<CSprite3DData>();



        public void FillDataStructure(object entity)
        {
            CEntity ent = entity as CEntity;
            if (ent == null)
                return;
            if (ent.Model != null)
            {
                modelData = CDataStructuresMgr.Instance.SaveItem(ent.Model) as AModelData;
            }

            if (ent.Physic != null)
            {
                physicObjectData = new CPhysicObjectData();//CDataStructuresMgr.Instance.SaveItem(ent.Physic) as CPhysicObjectData;
                physicObjectData.FillDataStructure(ent.Physic);
            }
            if (ent.Emitter.Count > 0)
            {
                foreach (AEmitter emitter in ent.Emitter.Values)
                {
                    CEmitterData data = CDataStructuresMgr.Instance.SaveItem(emitter) as CEmitterData;
                    if (data != null)
                        emitterData.Add(data);
                }
            }
            // TODO: Audio entities
#if !WINDOWS_PHONE
            if (ent.Sound.Count > 0)
            {
                foreach (CSound sound in ent.Sound.Values)
                {
                    CAudioEntityData data = new CAudioEntityData();
                    data.FillDataStructure(sound);
                    audioEntityData.Add(data);
                }
            }
#endif
            if (ent.Sprite3D != null)
            {
                foreach (CSprite3D spr3D in ent.Sprite3D.Values)
                {
                    CSprite3DData data = CDataStructuresMgr.Instance.SaveItem(spr3D) as CSprite3DData;
                    sprite3DData.Add(data);
                }
            }
        }

        public void FillFromDataStructure(out object entity, params object[] paramsData)
        {
            entity = new CEntity(paramsData[0] as CNode);


            if (modelData != null)
            {
                object m;
                CDataStructuresMgr.Instance.LoadItem(out m, modelData);
                if (m != null)
                    (entity as CEntity).AttachModel(m as AModel);
            }
            if (physicObjectData != null)
            {
                object p;
                CDataStructuresMgr.Instance.LoadItem(out p, physicObjectData);
                if (p != null)
                    (entity as CEntity).AttachPhysic(p as APhysicObject);
            }
            foreach (CEmitterData emitter in emitterData)
            {
                object e;
                CDataStructuresMgr.Instance.LoadItem(out e, emitter);
                if (e != null)
                    (entity as CEntity).AttachEmitter(e as AEmitter);
            }
#if !WINDOWS_PHONE
            foreach (CAudioEntityData audioEntity in audioEntityData)
            {
                object e;
                CDataStructuresMgr.Instance.LoadItem(out e, audioEntity);
                if (e != null)
                    (entity as CEntity).AttachSound(e as CSound);
            }
#endif
            foreach (CSprite3DData sprite3D in sprite3DData)
            {
                object e;
                CDataStructuresMgr.Instance.LoadItem(out e, sprite3D);
                if (e != null)
                    (entity as CEntity).AttachSprite3D(e as CSprite3D);
            }
        }
    }
}
